/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community;

import cn.ac.nercis.pes.community.constant.AppConstants;
import cn.ac.nercis.pes.community.support.SplashScreen;
import cn.ac.nercis.pes.community.support.svg.SvgGlyphLoader;
import cn.ac.nercis.pes.community.utils.AppUtils;
import cn.ac.nercis.pes.community.utils.GuiStore;
import cn.ac.nercis.pes.community.utils.SpringUtils;
import cn.ac.nercis.pes.service.utils.FileManage;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.event.EventUtils;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

/**
 * JavaFx入口
 * @author zpy
 * @version 0.5.0
 */
@Slf4j
public class ShellApplication extends Application {
    private static final SplashScreen SPLASH_SCREEN = new SplashScreen();

    @Override
    public void start(Stage primaryStage) throws Exception {
        loadIconFont();
        GuiStore.setStage(primaryStage);
        GuiStore.setHostServices(this.getHostServices());

        //展示欢迎页面
        Stage splashStage = new Stage(StageStyle.TRANSPARENT);
        Scene splashScene = new Scene(SPLASH_SCREEN.getParent(), javafx.scene.paint.Color.TRANSPARENT);
        splashStage.setScene(splashScene);
        splashStage.initStyle(StageStyle.TRANSPARENT);
        splashStage.getIcons().addAll(GuiStore.readIcons());
        splashStage.show();
        CompletableFuture.supplyAsync(()->{
            log.info("begin_start");
            FileManage.initWorkspace(AppUtils.getExecutiveBasePath());
            AppUtils.checkAndStartDocServer();
            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
            return new SpringApplicationBuilder(CommunityApplication.class).run();
        }).thenAcceptAsync(res->{
            SpringUtils.setApplicationContext(res);
            SpringUtils.getApplicationContext().publishEvent(new StageReadyEvent(primaryStage));
            log.info("start_finish");
            splashStage.hide();
            splashStage.setScene(null);
        },Platform::runLater).exceptionally(ex->{
            log.error("start error：",ex);
            return null;
        });
    }

    private void loadIconFont() throws IOException {
        try {
            ClassPathResource resource = new ClassPathResource("/icons/icon.svg");
            SvgGlyphLoader.loadGlyphsFont(resource.getInputStream(), AppConstants.ICON_FONT_KEY);
        }catch (IOException ioExc){
            log.error("加载图标文件失败！",ioExc);
            throw ioExc;
        }
    }

    static class StageReadyEvent extends ApplicationEvent {
        public StageReadyEvent(Stage stage) {
            super(stage);
        }

        public Stage getStage() {
            return (Stage) getSource();
        }
    }
}